package com.ant.create.controller;

import com.ant.common.annotation.AuthIgnore;
import com.ant.common.exceptions.BusinessException;
import com.ant.common.result.ResponseResult;
import com.ant.common.utils.StringUtil;
import com.ant.create.entity.CreateSet;
import com.ant.create.service.CreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

@RestController
@RequestMapping(value = "/create")
public class CreateController {

    @Autowired
    private CreateService createService;

    /**
     * 跳转设置页
     *
     * @return
     */
    @GetMapping(value = "/toCreateTable")
    @AuthIgnore
    public ModelAndView toIndex() {
        ModelAndView mv = new ModelAndView();
        CreateSet createSet = new CreateSet();
        createSet.setCodePackage("src/main/java/com/ant/business/other/");
        createSet.setAllPackage("com.ant.business.other.");
        mv.setViewName("create/create-table");
        mv.addObject("createSet", createSet);
        return mv;
    }

    /**
     * 保存
     *
     * @param createSet
     * @return
     */
    @PostMapping(value = "/save")
    @AuthIgnore
    public ResponseResult save(CreateSet createSet) {
        try {
            if (Objects.isNull(createSet)) {
                throw new BusinessException("设置不能为空");
            }

            if (Objects.isNull(createSet.getDatabaseName())) {
                throw new BusinessException("数据库不能为空");
            }

            if (StringUtil.isEmpty(createSet.getTableNames())) {
                throw new BusinessException("表名不能为空");
            }

            String codePackage = "src/main/java/com/ant/business/other/";
            String allPackage = "com.ant.business.other.";

            String getCodePackage = createSet.getCodePackage();

            if (StringUtil.isEmpty(getCodePackage)) {
                createSet.setCodePackage(codePackage);
            } else {
                String substring = getCodePackage.substring(getCodePackage.length() - 1, getCodePackage.length());
                if (!"/".equals(substring)) {
                    getCodePackage = getCodePackage + "/";
                    createSet.setCodePackage(getCodePackage);
                }
            }

            String getAllPackage = createSet.getAllPackage();
            if (StringUtil.isEmpty(getAllPackage)) {
                createSet.setAllPackage(allPackage);
            } else {
                String substring = getAllPackage.substring(getAllPackage.length() - 1);
                if (!".".equals(substring)) {
                    getAllPackage = getAllPackage + ".";
                    createSet.setAllPackage(getAllPackage);
                }
            }

            if (StringUtil.isEmpty(createSet.getTemplates())) {
                createSet.setTemplates("entity,mapper,service,serviceImpl,controller");
            }

            createService.createCode(createSet);
            return ResponseResult.success("代码生成成功");
        } catch (Exception e) {
            return ResponseResult.fail("代码生成失败：" + e.getMessage());
        }
    }

}
